<?php

namespace app\command;

use GuzzleHttp\Client;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use app\service\MediaAnalyzeService;


class DingTalkMsgPushCmd extends Command
{
    protected static $defaultName = 'msg:push';
    protected static $defaultDescription = '钉钉消息推送';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $timestamp = Carbon::now()->getPreciseTimestamp(3);
        $secretKey = 'SEC621dde1dd23e2969eeb4132658e42d7d2d09c519eea531e43489326cfd5a5aca';

        $stringToSign = $timestamp."\n".$secretKey;
        // 使用HMAC-SHA256算法和密钥生成签名
        $hmac_code = hash_hmac('sha256', $stringToSign, $secretKey, true);
        // 对HMAC结果进行base64编码，并转换为URL编码
        $signature = urlencode(base64_encode($hmac_code));

        $url = 'https://oapi.dingtalk.com/robot/send?access_token=f251e355d6a5b1d3006be27cd31e2bee24ff1fde2977340da500926b9eb746de&timestamp='.$timestamp.'&sign='.$signature;

        $payload = [
            'msgtype'=>'text',
            "text"=>['content'=>'你好']
        ];
        $client = new Client();
        $response = $client->post($url, ['verify' => false, 'timeout' =>5,'json'=>$payload]);
        $info = json_decode($response->getBody()->getContents(), true);

        $output->writeln('signature:'.$timestamp);
        $output->writeln(json_encode($info,JSON_UNESCAPED_UNICODE));
        return self::SUCCESS;
    }
}
